package spring.superrun.record.mvc.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Length;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import spring.superrun.record.mvc.dto.LocationAddDto;
import spring.superrun.record.mvc.dto.LocationUpdateDto;
import spring.superrun.record.mvc.entity.SuperrunLocations;
import spring.superrun.record.mvc.entity.enumlibs.ServiceResultCode;
import spring.superrun.record.mvc.entity.response.ServiceResult;
import spring.superrun.record.mvc.entity.response.ServiceResultOfT;
import spring.superrun.record.mvc.service.SuperrunLocationsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * (SuperrunLocations)表控制层
 *
 * @author makejava
 * @since 2021-06-16 16:05:05
 */
@RestController
@RequestMapping("api/locations")
@Api(tags = "位置管理接口")
public class LocationsController{
    /**
     * 服务对象
     */
    @Resource
    private SuperrunLocationsService superrunLocationsService;

    @GetMapping
    @ApiOperation("获取所有地点")
    public ResponseEntity<ServiceResultOfT<List<SuperrunLocations>>> GetLocations(){
        ServiceResultOfT<List<SuperrunLocations>> result = new ServiceResultOfT<List<SuperrunLocations>>();
        result.IsFailed(ServiceResultCode.NotFound,"未找到符合条件的位置信息");
        List<SuperrunLocations> entities =superrunLocationsService
                .queryAllByLimit(0,100);
        entities.sort((o1,o2)->o2.getOrderid().compareTo(o1.getOrderid()));
        result.setEntities(entities);
        if (result.getEntities()==null){
            return new ResponseEntity<>(result, HttpStatus.NOT_FOUND);
        }
        result.IsSuccess("成功获取到所有地点");
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    /**
     *通过id查询一个位置信息
     * @return ResponseEntity<ServiceResultOfT<SuperrunLocations>>
     */
    @GetMapping(value = "/{id}")
    @ApiOperation("获取某个位置信息根据id")
    public  ResponseEntity<ServiceResultOfT<SuperrunLocations>> GetLocation(@PathVariable String id){
        ServiceResultOfT<SuperrunLocations> result = new ServiceResultOfT<>();
        result.IsFailed(ServiceResultCode.NotFound,"未找到符合条件的位置信息");
        if (id==null||id.isEmpty()){
            result.IsFailed(ServiceResultCode.KeyIsNull,"为输入关键信息");
            return  new ResponseEntity<>(result,HttpStatus.BAD_REQUEST);
        }
        result.setEntities(superrunLocationsService.queryById(id));
        if (result.getEntities()==null){
            return new ResponseEntity<>(result,HttpStatus.NOT_FOUND);
        }
        result.IsSuccess("查询一个位置信息成功");
        return  new ResponseEntity<>(result,HttpStatus.OK);
    }
    @PostMapping
    @ApiOperation("添加一个位置信息")
    public ResponseEntity<ServiceResult> AddLocation(@RequestBody @Valid LocationAddDto dto){
        ServiceResult result = new ServiceResult();
        result.IsFailed("添加失败");
        if (dto==null){
            result.IsFailed(ServiceResultCode.KeyIsNull,"位置关键信息为空,添加失败");
            return new ResponseEntity<>(result,HttpStatus.BAD_REQUEST);
        }
        SuperrunLocations location = new SuperrunLocations();
        SuperrunLocations locationAdd = superrunLocationsService.insert(location.Add(dto));
        if (locationAdd==null){
            return  new ResponseEntity<>(result,HttpStatus.BAD_REQUEST);
        }
        result.IsSuccess("添加一个位置信息成功");
        return  new ResponseEntity<>(result,HttpStatus.OK);
    }
    @PutMapping
    @ApiOperation("更新一个位置信息")
    public ResponseEntity<ServiceResult> UpdateLocation(@RequestBody @Valid LocationUpdateDto dto){
        ServiceResult result = new ServiceResult();
        result.IsFailed("更新失败");
        if (dto.getId()==null||dto.getId().isEmpty()){
            result.IsFailed(ServiceResultCode.KeyIsNull,"为输入关键信息");
            return  new ResponseEntity<>(result,HttpStatus.BAD_REQUEST);
        }
        SuperrunLocations location = superrunLocationsService.queryById(dto.getId());
        if (location==null){
            return  new ResponseEntity<>(result,HttpStatus.BAD_REQUEST);
        }
        SuperrunLocations locationUpdate= superrunLocationsService.update(location.Update(dto));
        if (locationUpdate==null){
            return  new ResponseEntity<>(result,HttpStatus.BAD_REQUEST);
        }
        result.IsSuccess("更新成功");
        return new ResponseEntity<>(result,HttpStatus.OK);
    }
    @DeleteMapping(value = "/{id}")
    @ApiOperation("删除一个位置信息")
    public ResponseEntity<ServiceResult> DelLocation
            (@PathVariable @Length(min = 20,max = 50) String id){
        ServiceResult result = new ServiceResult();
        result.IsFailed("删除失败");
        if (id==null||id.isEmpty()){
            result.IsFailed(ServiceResultCode.KeyIsNull,"为输入关键信息");
            return  new ResponseEntity<>(result,HttpStatus.BAD_REQUEST);
        }
        boolean isDel = superrunLocationsService.deleteById(id);
        if (!isDel){
            return  new ResponseEntity<>(result,HttpStatus.BAD_REQUEST);
        }
        result.IsSuccess("删除成功");
        return new ResponseEntity<>(result,HttpStatus.OK);
    }
}
